package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.ProductCategoryDaoImpl;
import com.ysd.entity.ProductCategory;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;


public class AdminProductCategoryServlet extends BaseServlet {
	
	/**
	 * 查询所有产品分类
	 * localhost:8080/shangcheng/adminProductCategory?reqName=getProductCates
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getProductCates(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		ProductCategoryDaoImpl pcDao = new ProductCategoryDaoImpl();// 创建实现类对象
		int page = 0, size = 0;
		String p = request.getParameter("page");
		String s = request.getParameter("size");
		
		if(p==null||p.equals("")) {
			page = Integer.parseInt(p);
		}
		if(p==null||p.equals("")) {
			size = Integer.parseInt(s);
		}
		List<ProductCategory> pcList = pcDao.getProductCategoriesByPage((page-1)*size, size); //分页查到的产品分类集合
	
		// 发送ajax请求, 服务器基本都会返回json格式字符串
		
		response.setCharacterEncoding("UTF-8"); //设置响应的编码格式
		PrintWriter out = response.getWriter(); //out对象
		
		Gson gson = new Gson();
		String res = gson.toJson(pcList); //将pcList转化为json字符串
		
		out.print(res); // 响应数据
		
		out.flush();
		out.close();
	}
	
	/**
	 * 查询所有产品分类
	 * localhost:8080/shangcheng/adminProductCategory?reqName=getAllProductCates
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getAllProductCates(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		ProductCategoryDaoImpl pcDao = new ProductCategoryDaoImpl();// 创建实现类对象
		List<ProductCategory> pcList = pcDao.getAllProductCategories(); //查到的产品分类集合
	
		// 发送ajax请求, 服务器基本都会返回json格式字符串
		
		response.setCharacterEncoding("UTF-8"); //设置响应的编码格式
		PrintWriter out = response.getWriter(); //out对象
		
		Gson gson = new Gson();
		String res = gson.toJson(pcList.size()); //将pcList转化为json字符串
		
		out.print(res); // 响应数据
		
		out.flush();
		out.close();
	}

	/**
	 * localhost:8080/shangcheng/adminProductCategory?reqName=delProductCate
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delProductCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int cId = Integer.parseInt(request.getParameter("categoryId")); // 接受前台传来的参数
		ProductCategoryDaoImpl pcDao = new ProductCategoryDaoImpl();
		int n = pcDao.deleteProductCateById(cId); //调用删除的方法
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		if (n > 0) { // 删除成功
			out.print(Result.toClient(true, "删除成功", ""));// {"success":true,"message":"删除成功","remark":""}
		} else { // 删除失败
			out.print(Result.toClient(false, "删除失败", ""));// {"success":false,"message":"删除失败","remark":""}
		}
		
		out.flush();
		out.close();
	}
	
	/**
	 * localhost:8080/shangcheng/adminProductCategory?reqName=addProductCate
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addProductCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		ProductCategoryDaoImpl pcDao = new ProductCategoryDaoImpl();
		
		String productCategoryName = request.getParameter("asdf"); // 接受前台传来的参数  tongjintao
		if(productCategoryName!=null && !productCategoryName.equals("")) {
			List<ProductCategory> pcList = pcDao.getProductCategoryByName(productCategoryName);
			System.out.println(pcList);
			if(pcList.size() == 0) { // 没有查到, 可以添加
				ProductCategory pc = new ProductCategory(0, productCategoryName);
				System.out.println(pc);
				
				int n = pcDao.addProductCategory(pc);
				if (n > 0) {
					out.print(Result.toClient(true, "添加成功", ""));
				} else {
					out.print(Result.toClient(false, "添加失败", ""));
				}
			} else { // 查到了, 不能添加
				out.print(Result.toClient(false, "分类名已存在", ""));
			}
		}
		
		
		out.flush();
		out.close();
	}
	
	/**
	 * localhost:8080/shangcheng/adminProductCategory?reqName=getProductCateById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getProductCateById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		ProductCategoryDaoImpl pcDao = new ProductCategoryDaoImpl();
		int id = Integer.parseInt(request.getParameter("cId"));
		ProductCategory pc = pcDao.getProductCategoryById(id);
		
		Gson gson = new Gson();
		out.print(gson.toJson(pc));
		
		out.flush();
		out.close();
	}
	
	/**
	 * localhost:8080/shangcheng/adminProductCategory?reqName=updateProductCateById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateProductCateById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		ProductCategoryDaoImpl pcDao = new ProductCategoryDaoImpl();
		int productCategoryId = Integer.parseInt(request.getParameter("proCateId"));
		String productCategoryName = request.getParameter("proCateName");
		
		List<ProductCategory> pcList = pcDao.getProductCategoryByName(productCategoryName);
		if(pcList.size() == 0) { // 没有内容, 可以用
			ProductCategory pc = new ProductCategory(productCategoryId, productCategoryName);
			int n = pcDao.updateProductCategory(pc);
			if (n > 0) {
				out.print(Result.toClient(true, "修改成功", ""));
			} else {
				out.print(Result.toClient(false, "修改失败", ""));
			}
		} else { // 不用
			out.print(Result.toClient(false, "分类名已存在", ""));
		}
		
		
		
		
		out.flush();
		out.close();
	}
}
